<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单详情id" prop="orderDetailId">
        <el-input v-model="formData.orderDetailId" placeholder="请输入订单详情id" />
      </el-form-item>
      <el-form-item label="付款流水" prop="payLogId">
        <el-input v-model="formData.payLogId" placeholder="请输入付款流水" />
      </el-form-item>
      <el-form-item label="办理类型" prop="bizType">
        <el-select v-model="formData.bizType" placeholder="请选择办理类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="金额" prop="cost">
        <el-input v-model="formData.cost" placeholder="请输入金额" />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="formData.orderStatus" placeholder="请选择订单状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="沟通情况" prop="communicatType">
        <el-select v-model="formData.communicatType" placeholder="请选择沟通情况">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="formData.payStatus" placeholder="请选择支付状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAY_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMode">
        <el-select v-model="formData.payMode" placeholder="请选择支付方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAY_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="补差价状态" prop="makeupStatus">
        <el-select v-model="formData.makeupStatus" placeholder="请选择补差价状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MAKEUP_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择订单状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开票状态" prop="invoiceStatus">
        <el-radio-group v-model="formData.invoiceStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="人脸识别状态" prop="facialStatus">
        <el-select v-model="formData.facialStatus" placeholder="请选择人脸识别状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.FACIAL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接单状态" prop="acceptOrderStatus">
        <el-select v-model="formData.acceptOrderStatus" placeholder="请选择接单状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ACCEPT_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单取消状态" prop="orderCancelStatus">
        <el-select v-model="formData.orderCancelStatus" placeholder="请选择订单取消状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ORDER_CANCEL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus">
        <el-select v-model="formData.checkStatus" placeholder="请选择审核状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CHECK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="formData.payTime"
          type="date"
          value-format="x"
          placeholder="选择支付时间"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { OrderApi, OrderVO } from '@/api/stamp/order'

/** 订单 表单 */
defineOptions({ name: 'OrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  orderDetailId: undefined,
  payLogId: undefined,
  bizType: undefined,
  cost: undefined,
  orderStatus: undefined,
  communicatType: undefined,
  payStatus: undefined,
  payMode: undefined,
  makeupStatus: undefined,
  status: undefined,
  invoiceStatus: undefined,
  facialStatus: undefined,
  acceptOrderStatus: undefined,
  orderCancelStatus: undefined,
  checkStatus: undefined,
  payTime: undefined,
  remark: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderApi.getOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderVO
    if (formType.value === 'create') {
      await OrderApi.createOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderApi.updateOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orderDetailId: undefined,
    payLogId: undefined,
    bizType: undefined,
    cost: undefined,
    orderStatus: undefined,
    communicatType: undefined,
    payStatus: undefined,
    payMode: undefined,
    makeupStatus: undefined,
    status: undefined,
    invoiceStatus: undefined,
    facialStatus: undefined,
    acceptOrderStatus: undefined,
    orderCancelStatus: undefined,
    checkStatus: undefined,
    payTime: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>